<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2018/6/21
 * Time: 10:37
 */

namespace app\index\controller;

use app\index\Controller;
use think\Db;
use think\Validate;

/**
 * @title 银行卡（模块14）
 * @description 接口说明
 * @group 银行卡
 * @header name:model_id   default:05 desc:模块id
 * @groupremark 已使用错误码：1140001
 */
class BankCard extends Controller
{
    /**
     * @title 查询用户的所有银行卡
     * @description 接口说明
     * @author 李锐志
     * @url /index/bank_card/bank_card_query
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:string require:1  other: desc:用户id
     * @method ~
     * @return error_id: 1140001 验证不通过
     * @return error_id: 1140002 用户未添加银行卡
     */
    public function bank_card_query(){

        $data = input();

        $validate = new Validate([
            ['token','require','缺少token'],
            ['user_id','require','未指定用户'],
        ]);

        if(!$validate->check($data)){
            $msg['error_id']="1140001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $result = Db::name('bank_card')
            ->where('user_id',$data['user_id'])
            ->select();

        if($result){
            $msg['list'] = $result;
            $msg['message'] = '查询成功';
            return $this->success($msg);
        }else{
            $msg['error_id'] = '1140002';
            $msg['message'] = '该用户未添加银行卡';
            return $this->error($msg);
        }
    }

    /**
     * @title 添加银行卡
     * @description 接口说明
     * @author 李锐志
     * @url /index/bank_card/bank_card_add
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:用户id
     * @param name:user_name type:string require:1  other: desc:银行卡用户名
     * @param name:card_num type:string require:1  other: desc:卡号
     * @param name:bank_name type:string require:1  other: desc:开户银行支行
     * @method ~
     * @return error_id: 1140003 验证不通过
     * @return error_id: 1140004 用户未添加银行卡
     * @return error_id: 1140009 不能重复添加同一张银行卡
     */
    public function bank_card_add(){

        $data = input();

        $validate = new Validate([
            ['token','require','缺少token'],
            ['user_id','require','未指定用户'],
            ['user_name','require','未输入用户名'],
            ['card_num','require|length:16,19|number','未输入卡号|卡号长度有误|卡号有误'],
            ['bank_name','require','未输入开户银行支行'],
        ]);

        if(!$validate->check($data)){
            $msg['error_id']="1140003";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $data['create_time'] = time();

        $is_bank_card = Db::name('bank_card')
            ->where('user_id',$data['user_id'])
            ->where('card_num',$data['card_num'])
            ->find();
        if($is_bank_card){
            $msg['error_id'] = '1140009';
            $msg['message'] = '不能重复添加同一张银行卡';
            return $this->error($msg);
        }
        $data['user_name']=urldecode($data['user_name']);
        $data['bank_name']=urldecode($data['bank_name']);
        $result = Db::name('bank_card')
            ->insert($data);

        if($result){
            $msg['message'] = '添加成功';
            return $this->success($msg);
        }else{
            $msg['error_id'] = '1140004';
            $msg['message'] = '添加失败';
            return $this->error($msg);
        }
    }

    /**
     * @title 删除银行卡
     * @description 接口说明
     * @author 李锐志
     * @url /index/bank_card/bank_card_del
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:用户id
     * @param name:card_num type:string require:1  other: desc:卡号
     * @method ~
     * @return error_id: 1140005 该用户没有这张银行卡
     * @return error_id: 1140006 校验错误
     */
    public function bank_card_del(){

        $data = input();

        $validate = new Validate([
            ['token','require','缺少token'],
            ['user_id','require','未指定用户'],
            ['card_num','require','未输入卡号'],
        ]);

        if(!$validate->check($data)){
            $msg['error_id']="1140006";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        $result = Db::name('bank_card')
            ->where('card_num',$data['card_num'])
            ->where('user_id',$data['user_id'])
            ->delete();

        if($result){
            $msg['message'] = '删除成功';
            return $this->success($msg);
        }else{
            $msg['error_id'] = '1140005';
            $msg['message'] = '该用户没有这张银行卡';
            return $this->error($msg);
        }
    }

    /**
     * @title 设置默认支付银行卡
     * @description 接口说明
     * @author 李锐志
     * @url /index/bank_card/bank_card_set
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:用户id
     * @param name:card_num type:string require:1  other: desc:卡号
     * @method ~
     * @return error_id: 1140008 该用户没有这张银行卡
     * @return error_id: 1140007 校验错误
     */
    public function bank_card_set(){

        $data = input();

        $validate = new Validate([
            ['token','require','缺少token'],
            ['user_id','require','未指定用户'],
            ['card_num','require','未输入卡号'],
        ]);

        if(!$validate->check($data)){
            $msg['error_id']="1140007";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        //将当前的默认支付的银行卡设置为0
        Db::name('bank_card')
            ->where('user_id',$data['user_id'])
            ->where('default_pay',1)
            ->setField('default_pay',0);

        $result = Db::name('bank_card')
            ->where('user_id',$data['user_id'])
            ->where('card_num',$data['card_num'])
            ->setField('default_pay',1);

        if($result){
            $msg['message'] = '设置成功';
            return $this->success($msg);
        }else{
            $msg['error_id'] = '1140008';
            $msg['message'] = '该用户没有这张银行卡';
            return $this->error($msg);
        }
    }
}